//
//  ViewController.swift
//  LearnReactiveSwift
//
//  Created by LGW on 2017/9/14.
//  Copyright © 2017年 LGW. All rights reserved.
//

import UIKit
import ReactiveSwift
import Result
import ReactiveCocoa

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
 
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

//MARK:- 使用教程
extension ViewController{
  

}
//MARK:- 源码解析
extension ViewController{
    //MARK:- Observer
    func testBag() {
        
        //初始化Bags,并指定泛型类型为String
      var myBags = Bag<String>()
        //用来存储Bag中每个元素的Token的数组
      var bagsTokens = ContiguousArray<RemovalToken>()
        
        //往包中添加值
        for i in 0..<10{
            let token = myBags.insert("\(i)")
            bagsTokens.append(token)
        }
        
        print("================输出Token对象的Hash值================")
        for i in bagsTokens.indices.reversed(){
            let identifier = ObjectIdentifier(bagsTokens[i])
            print(identifier.hashValue)
        }
        
        
        print("================初始化后的myBags================")
        dump(myBags)
        
        print("================通过Token移除元素================")
        myBags.remove(using: bagsTokens[0])
        myBags.remove(using: bagsTokens[1])
  
        //获取StartIndex
          print("startIndex = \(myBags.startIndex)")
        //获取EndIndex
          print("endIndex = \(myBags.endIndex)")
        //获取Bag容器的迭代器
        var myBagsIterator = myBags.makeIterator()
        while let element = myBagsIterator.next() {
            print(element)
        }
        
        
    }
    
    //MARK:- Observer
    func testObserver() {
        let observer = Observer<String, NSError> { (event) in
            switch event{
            case let .value(v):
                print(v)
            case let .failed(error):
                print("错误事件\(error)")
            case  .completed:
                print("observer完成事件")
            case  .interrupted:
                print("observer中断事件")
            }
        }
        
        let observer1 = Observer<String, NSError>(value: {(value) in
             print(value)
        },failed:{(error)in
             print(error)
        },completed:{
             print("observer1-完成事件")
        }){
             print("observer1-完成事件")
        }
        
    }
    //MARK:- Event
    func testEvent() {
        let event:Event<Int, NSError> = .value(100)
        print("event.vlaue = \(event.value!)")
        print("event.isTerminating = \(event.isTerminating)")
        print("event.isCOmpleted = \(event.isCompleted)")
        
        let error = NSError(domain: "错误", code: 98764, userInfo: ["userInfo":"value"])
        let errorEvent:Event<String, NSError> = Event.failed(error)
        print("errorEvent.error = \(errorEvent.error!)")
        print("errorEvent.isTerminated = \(errorEvent.isTerminating)")
        print("errorEvent.isCompleted = \(errorEvent.isCompleted)")
        
        /*
         event.vlaue = 100
         event.isTerminating = false
         event.isCOmpleted = false
         errorEvent.error = Error Domain=错误 Code=98764 "(null)" UserInfo={userInfo=value}
         errorEvent.isTerminated = true
         errorEvent.isCompleted = false

         */
        
    }
    //MARK:- Map
 
    func testMap() {
      //映射
        let eventInteger: Event<Int, NSError> = .value(100)
        let mapEvent: Event<String, NSError> = eventInteger.map { (value) -> String in
            return "该闭包里面提供的映射规则：\(value)"
        }
        print(mapEvent.value)
        
        
    }
}
